package com.ruoyi;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.ruoyi.common.utils.ServletUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.TemplateSpec;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author: Alan
 * @date: 2020/8/6 21:02
 */
@Controller
@RequestMapping("myTest")
public class MyTestController {

    public static Logger log = LoggerFactory.getLogger(MyTestController.class);

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    ThymeleafProperties properties;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private ShiroDialect shiroDialect;

    /**
     * http://127.0.0.1/api/zTest/myTest/test/hello?aa=ewf151
     * @param aa
     * @return
     */
    @GetMapping("test/hello")
    @ResponseBody
    public String hello(HttpServletRequest request,String aa) {
        String token = request.getHeader("token");
        log.info("token:{}",token);
        return "hello-" + aa;
    }

    /**
     * 成功实现渲染文本并支持shiro
     * http://127.0.0.1/myTest/test/templateEngine
     * @return
     */
    @GetMapping("test/templateEngine")
    @ResponseBody
    public String testTemplateEngine(HttpServletRequest request, HttpServletResponse response, Model model){
        model.addAttribute("testAA","alan");
        String orgHtml = "<!DOCTYPE html>\n" +
                "<html  lang=\"zh\" xmlns:th=\"http://www.thymeleaf.org\" xmlns:shiro=\"http://www.pollix.at/thymeleaf/shiro\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "---[[${testAA}]]---<a class=\"btn btn-success\" onclick=\"$.operate.add(0)\" shiro:hasPermission=\"system:menu:add\"><i class=\"fa fa-plus\"></i> 新增</a><p th:text='${testAA}'></p>\n" +
                "</body>\n" +
                "</html>";
        WebContext webContext = new WebContext(request,response,request.getServletContext(), request.getLocale(),model.asMap());
        //手动渲染
        TemplateEngine engine=new TemplateEngine();
        engine.setDialect(shiroDialect);
        String render = engine.process(orgHtml,webContext);
        return render;
    }

    /**
     * hello为项目配置的模版目录下的文件名
     * @param request
     * @param response
     * @param model
     * @return
     */
    @GetMapping("test/testThy")
    @ResponseBody
    public String testThy(HttpServletRequest request, HttpServletResponse response, Model model){
        model.addAttribute("testAA","alan");
        String html= "";
        //手动渲染
        WebContext webContext = new WebContext(request,response,request.getServletContext(), request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("hello", webContext);
        return html;
    }

    public static void main(String[] args) {

        String template = "<p th:text='${title}'></p>---[[${title}]]---";
        HashMap<String, Object> map = new HashMap<>();
        map.put("title","hello world");
        Context context = new Context();
        context.setVariables(map);
        TemplateEngine engine=new TemplateEngine();
        String render = engine.process(template,context);
        System.out.println("渲染之后的字符串是:"+render);

    }

}
